
//
//  HYSpectrumView.m
//  LEVE
//
//  Created by 上官惠阳 on 2017/3/18.
//  Copyright © 2017年 dashuju. All rights reserved.
//

#import "HYSpectrumView.h"

@interface HYSpectrumView ()
@property (nonatomic, strong) NSMutableArray * levelArray;
@property (nonatomic) NSMutableArray * itemArray;
@property (nonatomic) CGFloat itemHeight;
@property (nonatomic) CGFloat itemWidth;

@end

@implementation HYSpectrumView
{
    UIView *_redPointView;
}
- (id)init
{
    NSLog(@"init");
    if(self = [super init]) {
        [self setup];
    }

    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    NSLog(@"initWithFrame");
    if (self = [super initWithFrame:frame]) {
        [self setup];
    }

    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    NSLog(@"awakeFromNib");
    [self setup];
}

- (void)setup
{
    NSLog(@"setup");

    self.itemArray = [NSMutableArray new];

    self.numberOfItems = 20;//偶数

    self.itemColor = [UIColor colorWithRed:241/255.f green:60/255.f blue:57/255.f alpha:1.0];

    self.itemHeight = CGRectGetHeight(self.bounds);
    self.itemWidth  = CGRectGetWidth(self.bounds);

//    self.timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.itemWidth*0.35, 0, self.itemWidth*0.3, self.itemHeight)];

    //小红点
    CGFloat pointWidth = 6.0;
    CGFloat timeWith = 40.0;
    UIView *redPointView = [[UIView alloc] initWithFrame:CGRectMake((self.itemWidth - timeWith)/2 - pointWidth,self.itemHeight - 25 - pointWidth/2 , pointWidth, pointWidth)];
    redPointView.backgroundColor = [UIColor redColor];
    redPointView.layer.masksToBounds = YES;
    redPointView.layer.cornerRadius = pointWidth/2;
    [self addSubview:redPointView];
    _redPointView = redPointView;

    self.timeLabel = [[UILabel alloc]initWithFrame:CGRectMake((self.itemWidth - timeWith)/2, self.itemHeight - 35, timeWith, 20)];
    self.timeLabel.text = @"";
    self.timeLabel.font = [UIFont systemFontOfSize:12];
    [self.timeLabel setTextColor:[UIColor whiteColor]];
    [self.timeLabel setTextAlignment:NSTextAlignmentCenter];
    [self addSubview:self.timeLabel];

    self.levelArray = [[NSMutableArray alloc]init];
    for(int i = 0 ; i < self.numberOfItems/2 ; i++){
        [self.levelArray addObject:@(1)];
    }
}

-(void)setItemLevelCallback:(void (^)())itemLevelCallback
{
    NSLog(@"setItemLevelCallback");

    _itemLevelCallback = itemLevelCallback;

    CADisplayLink *displaylink = [CADisplayLink displayLinkWithTarget:_itemLevelCallback selector:@selector(invoke)];
    displaylink.frameInterval = 6;
    [displaylink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];

    for(int i=0; i < self.numberOfItems; i++)
    {
        CAShapeLayer *itemline = [CAShapeLayer layer];
        itemline.lineCap       = kCALineCapButt;
        itemline.lineJoin      = kCALineJoinRound;
        itemline.strokeColor   = [[UIColor clearColor] CGColor];
        itemline.fillColor     = [[UIColor clearColor] CGColor];
//        [itemline setLineWidth:self.itemWidth*0.35 / self.numberOfItems];
        [itemline setLineWidth:2];//音量条的宽度
        itemline.strokeColor   = [self.itemColor CGColor];

        [self.layer addSublayer:itemline];
        [self.itemArray addObject:itemline];
    }
}
- (void)setLevel:(CGFloat)level
{
    level = (level+37.5)*3.2;
    if( level < 0 ) level = 0;
    if (level > 114) level = 0;
    NSLog(@"setLevel:%f",level);

    [self.levelArray removeObjectAtIndex:self.numberOfItems/2-1];
    [self.levelArray insertObject:@((level / 6) < 1 ? 1 : level / 6) atIndex:0];

    [self updateItems];
}


- (void)setText:(NSString *)text{
    self.timeLabel.text = text;
}


- (void)updateItems
{
    //NSLog(@"updateMeters");

    UIGraphicsBeginImageContext(self.frame.size);

//    int x = self.itemWidth*0.7/self.numberOfItems;
//    int z = self.itemWidth*0.3/self.numberOfItems;
//    int y = self.itemWidth*0.65 - z;

    int x = 2 * 2.5;//控制间隙
    int z = 3;
    int y = self.itemWidth*0.5 - z;

    for(int i=0; i < (self.numberOfItems / 2); i++) {

        UIBezierPath *itemLinePath = [UIBezierPath bezierPath];

        y += x;

        [itemLinePath moveToPoint:CGPointMake(y, (self.itemHeight - 35)/2+([[self.levelArray objectAtIndex:i]intValue]+1)*z/2)];

        [itemLinePath addLineToPoint:CGPointMake(y, (self.itemHeight - 35)/2-([[self.levelArray objectAtIndex:i]intValue]+1)*z/2)];

        CAShapeLayer *itemLine = [self.itemArray objectAtIndex:i];
        itemLine.path = [itemLinePath CGPath];
    }

    y = self.itemWidth*0.5 + z;

    for(int i = (int)self.numberOfItems / 2; i < self.numberOfItems; i++) {

        UIBezierPath *itemLinePath = [UIBezierPath bezierPath];

        y -= x;

        [itemLinePath moveToPoint:CGPointMake(y, (self.itemHeight - 35)/2+([[self.levelArray objectAtIndex:i-self.numberOfItems/2]intValue]+1)*z/2)];

        [itemLinePath addLineToPoint:CGPointMake(y, (self.itemHeight - 35)/2-([[self.levelArray objectAtIndex:i-self.numberOfItems/2]intValue]+1)*z/2)];

        CAShapeLayer *itemLine = [self.itemArray objectAtIndex:i];
        itemLine.path = [itemLinePath CGPath];

    }

    UIGraphicsEndImageContext();
}

@end
